<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<form class="cmxform" id="personCarForm" method="post"
	action="assortInfo/addPersonCar">
	<div>
		<div class="panel panel-default">
			<div class="panel-heading">
				<h5 class="panel-title">基本信息</h5>
			</div>
			<div class="panel-body">
				<div>
					<!-- 人行-->
					<div id="div_0">
						<p>
							<span class="style_btn">人行</span> <span class="style_input">
								<a class="btn btn-danger radius a_font addbtn" id="addUse2"
								onclick="addDiv(this,'0')"> 添加</a>
							</span>
						</p>
					</div>
					<!-- 车行-->
					<div id="div_1">
						<p>
							<span class="style_btn">车行</span> <span class="style_input">
								<a class="btn btn-danger radius a_font addbtn" id="addUse2"
								onclick="addDiv(this,'1')"> 添加</a>
							</span>
						</p>
					</div>
					<!-- 人车混行-->
					<div id="div_2">
						<p>
							<span class="style_btn">人车混行</span> <span class="style_input">
								<a class="btn btn-danger radius a_font addbtn" id="addUse2"
								onclick="addDiv(this,'2')"> 添加</a>
							</span>
						</p>
					</div>
				</div>
			</div>
		</div>
		<div style="width:100%;text-align:center; padding-top:20px">
			<div id="btn_div1">
				<button class="btn btn-danger radius my-btn"
					onclick="savePersonCarInfo(0)" type="button">提交</button>
				<button class="btn btn-danger radius my-btn"
					onclick="savePersonCarInfo(1)" type="button">保 存</button>
				<a class="btn btn-default radius" onClick="resetForms('personCarForm');">
					重置 </a> <br />
			</div>
			<div id="btn_div2" style="display:none;">
				<shiro:hasPermission name="permission:edit">
					<button id="modifyBtn" type="button"
						class="btn btn-danger radius my-btn" onclick="savePersonCarInfo(3)">启用编辑</button>
					<button class="btn btn-danger radius my-btn hidbtn"
						onclick="savePersonCarInfo(2)" type="button">保存修改</button>
					<a class="btn btn-default radius hidbtn"
						onClick="resetForms('personCarForm');">重置</a>
				</shiro:hasPermission>
			</div>
		</div>

	</div>
</form>

<!-- 模板div start -->
<!-- 人行div start -->
<div id="rx" style="display:none;">
	<div style="border:1px solid gray;padding:10px;margin:10px 0px;">
		<table cellpadding="10" cellspacing="5" width="100%">
			<tr>
				<td width="40"><span>位置</span></td>
				<td><input id="" name="entrySite" value="" /></td>
				<td colspan="2" valign="top" align="right"><input id="entityId_ids"
					name="id" hidden="hidden" /> <a href="javascript:void(0)"
					onclick="delTrDiv(this)"> <i class="icon-fullscreen"></i>
				</a></td>
			</tr>
			<tr>
				<td width="40"><span>朝向</span></td>
				<td><select name="entryOrientation"
					class="personCarSelect2" style="width:135px;">
						<option value="东">东</option>
						<option value="南">南</option>
						<option value="西">西</option>
						<option value="北">北</option>
				</select></td>
				<td width="40"><span>材料</span></td>
				<td><input id="" name="entryTexture" value="" /></td>
			</tr>
			<tr>
				<td width="40"><span>限高</span></td>
				<td><input id="" name="entryHeight" value="" /> m</td>
				<td width="40"><span>宽度</span></td>
				<td><input id="" name="entryWidth" value="" /> m</td>
			</tr>
		</table>
	</div>
</div>
<!-- 人行div end-->
<!-- 模板div end -->
<script type="text/javascript">
	var x;//load弹窗的index
	$(function() {
		$(".a_font").css("color", "#fff");
		//初始化表单
		mySubmit("personCarForm", initPersonCarFormData, personCarSuccess);
		getPersonCarData();
	});
	//获取建筑数量下的使用功能集合
	function getPersonCarData() {
		var obj;
		var url = "assortInfo/getPersonCarByBuild";
		//pid:建筑数量id 预留
		var fkProjectID = parent.parent.parent.projectID; //建筑id
		$.getJSON(url, {
			buildingNumId : fkProjectID
		}, function(data, status) {
				if (data.length>0) {
				var pArr = new Array(); //人行列表
				var cArr = new Array(); //车行列表
				var pcArr = new Array(); //人车混行列表
					var $t,v;
					//添加模板div
					for (var i = 0; i < data.length; i++) {
						if (data[i].entryType == 0) { //人行
							addDiv(obj, 0);
							pArr.push(data[i]);
						} else if (data[i].entryType == 1) { //车行
							addDiv(obj, 1);
							cArr.push(data[i]);
						} else { //人车混行
							addDiv(obj, 2);
							pcArr.push(data[i]);
						}
					}
					var arr = ["东","南","西","北"];
					var html="";
					//赋值
					$t = $("#div_0 table");
					for (var i = 0; i < $t.length; i++) {
						v = $t[i];
						$(v).find("input[name='entrySite']").val(pArr[i].entrySite);
						//需要从数据库中查出来。。。未完待续
						if(arr.indexOf(pArr[i].entryOrientation)==-1){
							html="<option value=\""+pArr[i].entryOrientation+"\">"+pArr[i].entryOrientation+"</option>"
							$(v).find("select[name='entryOrientation']").append(html);
						}
						
						$(v).find("select[name='entryOrientation']").val(pArr[i].entryOrientation).trigger("change");
						$(v).find("input[name='entryTexture']").val(pArr[i].entryTexture);
						$(v).find("input[name='entryHeight']").val(returnFloat(pArr[i].entryHeight));
						$(v).find("input[name='entryWidth']").val(returnFloat(pArr[i].entryWidth));
						$(v).find("input[name='id']").val(pArr[i].id);
					}
					var $r = $("#div_1 table");
					for (var i = 0; i < $r.length; i++) {
						v = $r[i];
						$(v).find("input[name='entrySite']").val(cArr[i].entrySite);
						
						if(arr.indexOf(cArr[i].entryOrientation)==-1){
							html="<option value=\""+cArr[i].entryOrientation+"\">"+cArr[i].entryOrientation+"</option>"
							$(v).find("select[name='entryOrientation']").append(html);
						}
						
						$(v).find("select[name='entryOrientation']").val(cArr[i].entryOrientation).trigger("change");
						$(v).find("input[name='entryTexture']").val(cArr[i].entryTexture);
						$(v).find("input[name='entryHeight']").val(returnFloat(cArr[i].entryHeight));
						$(v).find("input[name='entryWidth']").val(returnFloat(cArr[i].entryWidth));
						$(v).find("input[name='id']").val(cArr[i].id);
					}
					var $s = $("#div_2 table");
					for (var i = 0; i < $s.length; i++) {
						v = $s[i];
						$(v).find("input[name='entrySite']").val(pcArr[i].entrySite);
						
						if(arr.indexOf(pcArr[i].entryOrientation)==-1){
							html="<option value=\""+pcArr[i].entryOrientation+"\">"+pcArr[i].entryOrientation+"</option>"
							$(v).find("select[name='entryOrientation']").append(html);
						}
						
						$(v).find("select[name='entryOrientation']").val(pcArr[i].entryOrientation).trigger("change");
						$(v).find("input[name='entryTexture']").val(pcArr[i].entryTexture);
						$(v).find("input[name='entryHeight']").val(returnFloat(pcArr[i].entryHeight));
						$(v).find("input[name='entryWidth']").val(returnFloat(pcArr[i].entryWidth));
						$(v).find("input[name='id']").val(pcArr[i].id);
					}
					//设置输入框为只读
					$("#personCarForm").find("input,textarea").attr("readonly", true);
					//隐藏按钮
					$("#personCarForm #btn_div2").show();
					$("#personCarForm #btn_div1").hide();
					$("#personCarForm .icon-fullscreen").hide();
					$("#personCarForm").find("select,.addbtn").attr("disabled", "disabled");
				}else{
					$("#personCarForm").find("select,.addbtn").removeAttr("disabled");
				}

		});
	}

	var optPersonCarInfo;
	//optPersonCarInfo操作字段，0：临时保存；1：新增；2：修改
	function savePersonCarInfo(val) {
		optPersonCarInfo = val;
		if (val == 3) {
			//解除锁定状态
			var index = layer.confirm('确定开启编辑状态？', {
				offset : '20%',
				shade : 0
			}, function() {
				//去除只读开启按钮编辑
				$("#personCarForm").find("input,textarea").attr("readonly", false);
				$("#personCarForm #modifyBtn").hide();
				// $(".hidbtn").removeClass("hidbtn");
				$("#personCarForm .hidbtn").addClass("showbtn");
				$("#personCarForm .icon-fullscreen").show();
				$("#personCarForm").find("select,.addbtn").removeAttr("disabled");
				layer.close(index);
			});
		} else {
			$("#personCarForm").submit();
		}
	}



	function initPersonCarFormData() {
		x=layer.load(2);
		var fkProjectid = parent.parent.parent.projectID; //建筑id
		var arr = new Array();
		//人行
		var $table = $("#div_0 table");
		var entryType = 0;
		for (var i = 0; i < $table.length; i++) {
			var v = $table[i];
			var entrySite = $(v).find("input[name='entrySite']").val();
			var entryOrientation = $(v).find("select[name='entryOrientation']").val();
			var entryTexture = $(v).find("input[name='entryTexture']").val();
			var entryHeight = $(v).find("input[name='entryHeight']").val();
			var entryWidth = $(v).find("input[name='entryWidth']").val();
			var cId = $(v).find("input[name='id']").val();
			if (!testNum(entryHeight)) {
				layer.msg("人行第" + (i + 1) + "行高度请输入数值");
				$(v).find("input[name='entryHeight']").focus();
				layer.close(x);
				return null;
			} else if (!testNum(entryWidth)) {
				layer.msg("人行第" + (i + 1) + "行宽度请输入数值");
				$(v).find("input[name='entryWidth']").focus();
				layer.close(x);
				return null;
			}
			arr.push({
				id:cId,
				entryType : entryType,
				entrySite : entrySite,
				entryOrientation : entryOrientation,
				entryTexture : entryTexture,
				entryHeight : entryHeight,
				entryWidth : entryWidth,
				fkProjectid : fkProjectid
			});
		}
		//车行
		var $table = $("#div_1 table");
		var entryType = 1;
		for (var i = 0; i < $table.length; i++) {
			var v = $table[i];
			var pId = $(v).find("input[name='id']").val();
			var entrySite = $(v).find("input[name='entrySite']").val();
			var entryOrientation = $(v).find("select[name='entryOrientation']").val();
			var entryTexture = $(v).find("input[name='entryTexture']").val();
			var entryHeight = $(v).find("input[name='entryHeight']").val();
			var entryWidth = $(v).find("input[name='entryWidth']").val();
			if (!testNum(entryHeight)) {
				layer.msg("车行第" + (i + 1) + "行高度请输入数值");
				$(v).find("input[name='entryHeight']").focus();
				layer.close(x);
				return null;
			} else if (!testNum(entryWidth)) {
				layer.msg("车行第" + (i + 1) + "行宽度请输入数值");
				$(v).find("input[name='entryWidth']").focus();
				layer.close(x);
				return null;
			}
			arr.push({
				id:pId,
				entryType : entryType,
				entrySite : entrySite,
				entryOrientation : entryOrientation,
				entryTexture : entryTexture,
				entryHeight : entryHeight,
				entryWidth : entryWidth,
				fkProjectid : fkProjectid
			});
		}
		//人车混行
		var $table = $("#div_2 table");
		var entryType = 2;
		for (var i = 0; i < $table.length; i++) {
			var v = $table[i];
			var pcId = $(v).find("input[name='id']").val();
			var entrySite = $(v).find("input[name='entrySite']").val();
			var entryOrientation = $(v).find("select[name='entryOrientation']").val();
			var entryTexture = $(v).find("input[name='entryTexture']").val();
			var entryHeight = $(v).find("input[name='entryHeight']").val();
			var entryWidth = $(v).find("input[name='entryWidth']").val();
			if (!testNum(entryHeight)) {
				layer.msg("人车混行第" + (i + 1) + "行高度请输入数值");
				$(v).find("input[name='entryHeight']").focus();
				layer.close(x);
				return null;
			} else if (!testNum(entryWidth)) {
				layer.msg("人车混行第" + (i + 1) + "行宽度请输入数值");
				$(v).find("input[name='entryWidth']").focus();
				layer.close(x);
				return null;
			}
			arr.push({
				id:pcId,
				entryType : entryType,
				entrySite : entrySite,
				entryOrientation : entryOrientation,
				entryTexture : entryTexture,
				entryHeight : entryHeight,
				entryWidth : entryWidth,
				fkProjectid : fkProjectid
			});
		}

		return {
			delCarIds:delCarIds,
			pcList:arr,
			opt:optPersonCarInfo
		};
	}
	function personCarSuccess(data, status) {
		if (status == "success") {
			layer.alert("保存成功！",{
				offset:'20%',
				shade:[0.1,'#fff']
			},function(){
				getPersonCarData();
				$("#personCarForm #div_0>div").remove();
				$("#personCarForm #div_1>div").remove();
				$("#personCarForm #div_2>div").remove();
				$("#personCarForm").find("#modifyBtn").show();
				$("#personCarForm").find(".hidbtn").removeClass("showbtn");
				layer.closeAll();
			});
			
			layer.close(x);
			
		} else {
			layer.alert('保存失败！');
			layer.close(x);
		}
	}
	//统计某个id下所有table中的表单数据
	//入口类别0:人行、1:车行、2:人车混行
	/* function personCarTableData(id,entryType) {
		var arr=new Array();
		
		return arr;
	} */

	function addDiv(obj, i) {
		var node = $("#rx").html();
		var oTest = $("#div_" + i);

		oTest.append(node);
		//添加元素之后，激活selece2插件
		oTest.find(".personCarSelect2").select2({
			tags : true
		});
	}
	
	//存储将要删除的id，传到后台删除
	var delCarIds = new Array();
	function delTrDiv(obj) {
		var aId = $(obj).parent().parent().parent().parent().parent().find("input[name='id']").val();
		delCarIds.push(aId);
		$(obj).parent().parent().parent().parent().parent().remove();
		layer.msg("点击保存后，将彻底删除该条数据！");
	}

	/* function delTrDiv(obj) {
			var aId = $(obj).parent().parent().parent().parent().parent().find("input[name='id']").val();
			 layer.confirm('确定删除该条信息吗？', {
				btn : [ '确定', '取消' ] //按钮
			}, function() {
				//点击确定时，删除该条数据
				if (aId == "") {
					$(obj).parent().parent().parent().parent().parent().remove();
					layer.msg("删除成功");
				} else {
					$.ajax({
						url : "assortInfo/delPersonCar", //请求的url地址
						async : true, //请求是否异步，默认为异步，这也是ajax重要特性
						data : {
							"id" : aId
						}, //参数值
						type : "GET", //请求方式
	
						success : function(req) {
							if (req.success) {
								layer.msg("删除成功");
								$(obj).parent().parent().parent().parent().parent().remove();
							}
						}
					});
				}
			}, function() {
				layer.close();
			}); 
	
	} */
</script>

